﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using JBotAPI.Logs;

namespace JBot {
	/// <summary>
	/// Interaction logic for ConsoleControl.xaml
	/// </summary>
	public partial class ConsoleControl : UserControl {
		public ConsoleControl( ) {
			this.InitializeComponent( );
			App.xmpp.Logs.LogAdded += new LogCollection.NewLogEvent(PrintLog);
		}

		private void SendCommand(string tekst) {
			if (tekst.Length > 0) {
				/*if (this.TB_Console.AutoCompleteCustomSource.IndexOf(tekst) < 0)
					this.TB_Console.AutoCompleteCustomSource.Add(tekst);*/
				char prefix = tekst[0];
				string commads;
				if (tekst.IndexOf(" ") >= 0)
					commads = tekst.Remove(0, 1).Remove(tekst.IndexOf(" "));
				else
					commads = tekst.Remove(0, 1);
				string[ ] parms = tekst.Remove(0, tekst.IndexOf(" ") + 1).Split(' ');
				switch (prefix) {
					case '#':
						this.richTextBoxConsoleOutput.AppendText("\n" + "console:~# " + tekst.Remove(0, 1));
						if (commads == "connect") {
							if (App.xmpp.XmppConnectionState.ToString( ) == "Disconnected")
								App.xmpp.connect( );
							/*else
								FormMain.Loging("Bot is " + FormMain.xmpp.XmppConnectionState.ToString() + "!", 100);*/
						}
						if (commads == "disconnect") {
							if (App.xmpp.XmppConnectionState.ToString( ) != "Disconnected")
								/*FormMain.Loging("Bot is " + FormMain.xmpp.XmppConnectionState.ToString() + "!", 100);
							else*/
								App.xmpp.disconnect( );
						}
						break;
					case '$':
						this.richTextBoxConsoleOutput.AppendText("\n" + "console:~$ " + tekst.Remove(0, 1));
						break;
					case '!':
						this.richTextBoxConsoleOutput.AppendText("\n" + "console:~! " + tekst.Remove(0, 1));
						break;
					case '?':
						this.richTextBoxConsoleOutput.AppendText("\n" + "console:?> " + tekst.Remove(0, 1));
						break;
					case '@':
						this.richTextBoxConsoleOutput.AppendText("\n" + "console:@> " + tekst.Remove(0, 1));
						break;
					case '.':
						this.richTextBoxConsoleOutput.AppendText("\n" + "console:.> " + tekst.Remove(0, 1));
						break;
					default:
						this.richTextBoxConsoleOutput.AppendText("\n" + "console:^> " + tekst);
						break;
				}
			}
			this.richTextBoxConsoleOutput.ScrollToEnd( );
		}
		public void PrintLog(object sender, NewLogEventArgs e) {
			if (!this.Dispatcher.CheckAccess( )) {
				this.Dispatcher.Invoke(new LogCollection.NewLogEvent(PrintLog), DispatcherPriority.Normal, new object[ ] { sender, e });
			} else {
				if (Array.IndexOf(new LogFlags[ ] { LogFlags.XMPP, LogFlags.Script, LogFlags.Program, LogFlags.Bot, LogFlags.Console, LogFlags.ConsoleResult }, e._Flag) != -1) {
					//this.richTextBoxConsoleOutput.AppendText(String.Format("\n[{0} {1}{2}]: {3}", e._Flag.ToString( ), e._Type.ToString( ), String.Format(" at {0}", DateTime.Now.ToLongTimeString( )), e._Text));
					Paragraph paragraph = new Paragraph( );
					Brush brushTimeStamp;
					switch (e._Type) {
						case LogType.Error:
							brushTimeStamp = Brushes.Red;
							break;
						case LogType.Info:
							brushTimeStamp = Brushes.DeepSkyBlue;
							break;
						default:
							brushTimeStamp = Brushes.Gold;
							break;
					}
					paragraph.Inlines.Add(new Span(new Run("[")) {
						Foreground = Brushes.Gold
					});
					paragraph.Inlines.Add(new Span(new Run(DateTime.Now.ToLongTimeString( ) + " - ")) {
						Foreground = Brushes.Gold
					});
					paragraph.Inlines.Add(new Span(new Run(String.Format("{1} {2}", DateTime.Now.ToLongTimeString( ), e._Flag.ToString( ), e._Type.ToString( )))) {
						Foreground = brushTimeStamp
					});
					paragraph.Inlines.Add(new Span(new Run("]: ")) {
						Foreground = Brushes.Gold
					});
					paragraph.Inlines.Add(new Run(e._Text));
					this.richTextBoxConsoleOutput.Document.Blocks.Add(paragraph);
					//this.richTextBoxConsoleOutput.Measure(new Size(); <-- funkcja bardzo zamulająca !!!
					this.richTextBoxConsoleOutput.ScrollToEnd( );
				}
			}
		}

		/*#region Zdarzenia okna
		private void TB_Console_KeyDown(object sender, KeyEventArgs e) {
			if (Key.Enter == e.Key) {
				this.SendCommand(this.TB_Console.Text);
				this.TB_Console.Clear( );
			}
		}
		private void CMS_Opening(object sender, CancelEventArgs e) {
			ContextMenu CMS_Tekst_V = (ContextMenu)sender;
			RichTextBox CMS_Tekst_P = (RichTextBox)CMS_Tekst_V.PlacementTarget;
			CMS_Tekst_V.Items[0].Visibility = !CMS_Tekst_P.IsReadOnly;
			CMS_Tekst_V.Items[1].Visibility = !CMS_Tekst_P.IsReadOnly;
			CMS_Tekst_V.Items[2].Visibility = !CMS_Tekst_P.IsReadOnly;
			CMS_Tekst_V.Items[5].Visibility = !CMS_Tekst_P.IsReadOnly;
			if (CMS_Tekst_P.Selection.IsEmpty) {
				CMS_Tekst_V.Items[3].Visibility = false;
				CMS_Tekst_V.Items[4].Visibility = false;
				CMS_Tekst_V.Items[6].Visibility = false;
				CMS_Tekst_V.Items[7].Visibility = !CMS_Tekst_P.IsReadOnly;
				CMS_Tekst_V.Items[8].Visibility = true;
				CMS_Tekst_V.Items[9].Visibility = false;
			} else {
				CMS_Tekst_V.Items[3].Visibility = true & !CMS_Tekst_P.IsReadOnly;
				CMS_Tekst_V.Items[4].Visibility = true;
				CMS_Tekst_V.Items[6].Visibility = true & !CMS_Tekst_P.IsReadOnly;
				CMS_Tekst_V.Items[7].Visibility = true;
				if (CMS_Tekst_P.Selection.Text.Length == CMS_Tekst_P.ToString().Length) {
					CMS_Tekst_V.Items[8].Visibility = false;
					CMS_Tekst_V.Items[9].Visibility = true;
				} else {
					CMS_Tekst_V.Items[8].Visibility = true;
					CMS_Tekst_V.Items[9].Visibility = true;
				}
			}
		}
		private void CMS_RTB_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
			ContextMenu CMS_Tekst_V = (ContextMenu)sender;
			RichTextBox CMS_Tekst_P = (RichTextBox)CMS_Tekst_V.SourceControl;
			if (CMS_Tekst_P == null) {
				try {
					RichTextBox activeBox = RTB_Serch(this.Controls) as RichTextBox;
					if (activeBox != null && activeBox.ContainsFocus) {
						CMS_Tekst_P = activeBox;
					}
				} catch { }
			}
			if (CMS_Tekst_P != null) {
				if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 0) {
					RTB_ItemUnDo(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 1) {
					RTB_ItemReDo(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 3) {
					RTB_ItemCut(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 4) {
					RTB_ItemCopy(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 5) {
					RTB_ItemPaste(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 6) {
					RTB_ItemDelete(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 8) {
					RTB_ItemSelectAll(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 9) {
					RTB_ItemUnSelectAll(CMS_Tekst_P);
				}
			}
		}
		private Control RTB_Serch(ControlCollection ctrl) {
			if (ctrl != null) {
				foreach (Control ctrlin in ctrl) {
					try {
						RichTextBox activeBox3 = ctrlin as RichTextBox;
						if (activeBox3 != null && activeBox3.ContainsFocus) {
							return ctrlin;
						}
					} catch { }
					if (ctrlin.Controls != null) {
						Control where = RTB_Serch(ctrlin.Controls);
						if (where != null) {
							return where;
						}
					}
				}
			}
			return null;
		}
		private void RTB_ItemUnDo(RichTextBox RTB) {
			if (RTB != null)
				RTB.Undo( );
		}
		private void RTB_ItemReDo(RichTextBox RTB) {
			if (RTB != null)
				RTB.Redo( );
		}
		private void RTB_ItemCut(RichTextBox RTB) {
			if (RTB != null)
				RTB.Cut( );
		}
		private void RTB_ItemCopy(RichTextBox RTB) {
			if (RTB != null)
				RTB.Copy( );
		}
		private void RTB_ItemPaste(RichTextBox RTB) {
			if (RTB != null)
				RTB.Paste( );
		}
		private void RTB_ItemDelete(RichTextBox RTB) {
			if (RTB != null) {
				string temp = Clipboard.GetText( );
				RTB.Cut( );
				Clipboard.SetText(temp);
			}
		}
		private void RTB_ItemSelectAll(RichTextBox RTB) {
			if (RTB != null)
				RTB.SelectAll( );
		}
		private void RTB_ItemUnSelectAll(RichTextBox RTB) {
			if (RTB != null)
				RTB.Selection.Select(RTB.CaretPosition, RTB.CaretPosition);
		}
		#endregion*/
	}
}